package org.tea.inventory.admin.web.customer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tea.inventory.admin.web.admin.BaseController;
import org.tea.inventory.base.contants.UserConstants;
import org.tea.inventory.base.exception.ApplicationException;
import org.tea.inventory.base.response.BaseResponse;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.base.util.StringUtils;
import org.tea.inventory.base.util.excel.ExportExcel;
import org.tea.inventory.base.validation.groups.customer.BusinessGroup;
import org.tea.inventory.domain.tea.CustomerBig;
import org.tea.inventory.domain.tea.Product;
import org.tea.inventory.domain.tea.WarehouseInventorySerialInfo;
import org.tea.inventory.service.ICustomerBusinessService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业客户 Controller
 *
 * @author dongxb
 * @date 2025-04-16
 */
@Api(tags = "客户管理-企业客户管理控制器")
@Slf4j
@RestController
@RequestMapping("/admin/customer-business")
@RequiredArgsConstructor
public class CustomerBusinessController extends BaseController
{
    @Autowired
    private ICustomerBusinessService customerBusinessService;

    /**
     * 查询企业客户列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "查询企业客户列表")
    @ApiResponses(
            @ApiResponse(code = 0, message = "成功", response = Product.class)
    )
    public BaseResponse page(CustomerBig customerBig)
    {
        startPage();
        List<CustomerBig> list = customerBusinessService.selectCustomerBigList(customerBig);
        return getDataTable(list);
    }

    /**
     * 导出企业客户列表
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出企业客户列表")
    public void export(CustomerBig customerBig, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        String title = "企业客户列表";
        String header = "企业编号,企业名称,联系人,联系电话,状态";
        String column = "customerCode,companyName,contactsName,contactsPhone,statusStr";
        map.put("biaoti", title);
        map.put("header", header);
        map.put("column", column);
        try {
            List<CustomerBig> list = customerBusinessService.selectCustomerBigList(customerBig);
            List<Map> listMap = JSONArray.parseArray(JSON.toJSONString(list), Map.class);
            ExportExcel.export(request, response, listMap, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取企业客户详细信息
     */
    @GetMapping("/info")
    @ApiOperation(value = "获取企业客户详细信息")
    public BaseResponse getInfo(Long id)
    {
        return ResponseUtil.ok(customerBusinessService.selectCustomerBigById(id));
    }

    /**
     * 新增企业客户
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增企业客户")
    public BaseResponse add(@Validated({BusinessGroup.class}) @RequestBody CustomerBig customerBig)
    {
        if (!checkCodeOrNameUnique(customerBig)) throw new ApplicationException(-1, "新增企业客户失败：企业商编号或企业名称已经存在。");
        return ResponseUtil.ok(customerBusinessService.insertCustomerBig(customerBig));
    }

    /**
     * 修改企业客户
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "修改企业客户")
    public BaseResponse edit(@Validated({BusinessGroup.class}) @RequestBody CustomerBig customerBig)
    {
        if (!checkCodeOrNameUnique(customerBig)) throw new ApplicationException(-1, "修改企业客户失败：企业商编号或企业名称已经存在。");
        return ResponseUtil.ok(customerBusinessService.updateCustomerBig(customerBig));
    }

    public boolean checkCodeOrNameUnique(CustomerBig customer) {
        List<CustomerBig> list = customerBusinessService.selectCustomerBigByCodeOrName(customer);
        if (StringUtils.isNull(list) || list.size() == 0) {
            return UserConstants.UNIQUE;
        }
        return UserConstants.NOT_UNIQUE;
    }

    /**
     * 更新企业客户状态
     *
     * @param id 企业客户主键
     * @param status 企业客户更新前状态
     * @return 结果
     */
    @PostMapping(value = "/updateStatus")
    @ApiOperation(value = "更新企业客户状态")
    public BaseResponse updateStatus(@ApiParam(value = "更新id") Long id , @ApiParam(value = "更新前状态") Integer status)
    {
        return ResponseUtil.ok(customerBusinessService.updateStatus(id , status));
    }

    /**
     * 删除企业客户
     */
    @PostMapping(value = "/remove")
    @ApiOperation(value = "删除企业客户")
    public BaseResponse remove(@ApiParam(value = "删除企业客户id集合") Long id)
    {
        return ResponseUtil.ok(customerBusinessService.deleteCustomerBigById(id));
    }
}
